const express=require("express");
const router=express.Router();
const db=require("../db.js");
const tr = require('../tokenRule.js')

// 定义上传图片的路由
router.post('/add',tr.verifyToken,(req, res) => {
  let { star, suggest } = req.body
  let sqls="insert into star(star,suggest) values(?,?)";
  let objs=[star,suggest]
  db.query(sqls,objs,(err,data)=>{
  	if(err){
  		res.send({msg:err.message});
  		return;
  	}else{
  		res.send({
  			msg:"success",
  			code:200,
  		})
  	}
  })
});
router.get('/select',tr.verifyToken,(req,res)=>{
	let sql="select * from star"
	db.query(sql,(err,data)=>{
		if(err){
			res.send({msg:err.message});
			return;
		}else{
			res.send({
				msg:"success",
				data:data,
				code:200,
			})
		}
	})
})
// 编辑
router.post("/upd",tr.verifyToken,(req,res)=>{
	let sql="update star set starName=?,starjieshao=? where id=?";
	let sj=[req.body.star,req.body.suggest,req.body.id]
	db.query(sql,sj,(err,data)=>{
		if(err){
			res.send({msg:err.message});
			return;
		}else{
			res.send({
				msg:"修改成功",
				data:null,
				code:200,
			})
		}
	})
})
// 删除
router.post("/del",tr.verifyToken,(req,res)=>{
	let sql="delete from star where id=?";
	db.query(sql,req.body.id,(err,data)=>{
		if(err){
			res.send({msg:err.message});
			return;
		}else{
			res.send({
				msg:"删除成功",
				data:null,
				code:200,
			})
		}
	})
})

module.exports=router